//
//  YH_DisclaimerViewController.m
//  YinHang
//
//  Created by 123 on 14-6-20.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "YH_DisclaimerViewController.h"
#import "YH_RegisterViewController.h"

@interface YH_DisclaimerViewController ()
{
    ASIFormDataRequest *disclaimerRequest;
    
    __weak IBOutlet UIWebView *mainView;
    NSString *content;
    MBProgressHUD *postHud;
}
@end

@implementation YH_DisclaimerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"引航网";
    [self setUI];
    
    
    // 检查网络
    if (![Constants isExistenceNetwork]) {
        
    } else {
        if (!postHud || postHud.hidden == YES) {
            // 提示正在加载数据...
            postHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            postHud.labelText = @"正在获取协议，请稍后...";
        }
        /* 获取协议 */
        disclaimerRequest = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:API_STR]];
        disclaimerRequest.requestMethod = @"POST";
        // 设置超时时间
        disclaimerRequest.timeOutSeconds = 30;
        disclaimerRequest.delegate = self;
        // 设置参数
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, @"getxieyi",@""];
        
        NSData *paramsData = [result toGBKEncodingData];
        disclaimerRequest.postBody = [paramsData mutableCopy];
        
        [disclaimerRequest startAsynchronous];
    }
    
    isRead = NO;
}

- (void)setUI
{
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = - 10;
    if (ios7) {
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftBarButtonItem];
    }else{
        self.navigationItem.leftBarButtonItems = @[leftBarButtonItem];
    }

    
    
    UIImage *registerBtn_bgImg = [UIImage imageNamed:@"disclaimer_registerbtn"];
    [_registerBtn setBackgroundImage:[registerBtn_bgImg stretchableImageWithLeftCapWidth:registerBtn_bgImg.size.width*0.5f topCapHeight:registerBtn_bgImg.size.height*0.5f] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ASIHttpRequestDelegate method

- (void)requestFinished:(ASIHTTPRequest *)request {
    postHud.hidden = YES;
    NSLog(@"%@",request.responseString);
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[request.responseString dataForUTF8] options:NSJSONReadingAllowFragments error:&err];
    
    // 1. 是否解析有错误
    if (!err) {
        
        // 2. 判断错误码
        NSInteger errorCode = [result[@"errorCode"]integerValue];
        if (errorCode == 0) {
            NSDictionary *info = result[@"info"];
            content = info[@"xieyi"];
            NSString *contStr = [NSString stringWithFormat:@"<body style='background-color:#FFFFFF;color:#000000;font-size:%@' >%@</body>",@"14",content] ;
            [mainView loadHTMLString:contStr baseURL:nil];
        } else {
            [SVProgressHUD showErrorWithStatus:result[@"errorMsg"]];
        }
        
    } else {
        [SVProgressHUD showErrorWithStatus:@"数据解析出错！"];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    postHud.hidden = YES;
    [SVProgressHUD showErrorWithStatus:@"服务器无法访问，请稍后再试!"];
}


// 是否已经阅读免责声明
- (IBAction)isAlreadyReadDisclaimer:(id)sender {
    UIButton *btn = (UIButton*)sender;
    
    if (isRead == NO) {
        [btn setBackgroundImage:[UIImage imageNamed:@"choosebox_selected"] forState:UIControlStateNormal];
        isRead = YES;
    } else {
        [btn setBackgroundImage:[UIImage imageNamed:@"choosebox"] forState:UIControlStateNormal];
        isRead = NO;
    }
}

// 立即注册
- (IBAction)registerAtOnce:(id)sender {
    
    if (isRead == NO) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请先阅读并同意《引航网免责条例》" message:@"http://www.yinhang.hk" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        [disclaimerRequest cancel];
        
        YH_RegisterViewController *registerVC = [[YH_RegisterViewController alloc]initWithNibName:(IPHONE5?@"YH_RegisterViewController_4":@"YH_RegisterViewController") bundle:nil];
        [self.navigationController pushViewController:registerVC animated:YES];
    }
}

// 返回
- (void)back {
    self.navigationController.navigationBarHidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
}

@end
